package com.bus.core.entity;

import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.Table;

import com.bus.common.hibernate4.PriorityInterface;
import com.bus.core.entity.base.BaseBusUser;

@Entity
@Table(name="jc_user")
public class BusUser extends BaseBusUser implements PriorityInterface {
	private static final long serialVersionUID = 1L;
	public static final Integer USER_STATU_CHECKED = 0;
	public static final Integer USER_STATU_DISABLED = 1;
	public static final Integer USER_STATU_CHECKING = 2;

	public Byte getCheckStep(Integer siteId) {
		BusUserSite us = getUserSite(siteId);
		if (us != null) {
			return getUserSite(siteId).getCheckStep();
		} else {
			return null;
		}
	}

	public String getRealname() {
		BusUserExt ext = getUserExt();
		if (ext != null) {
			return ext.getRealname();
		} else {
			return null;
		}
	}

	public Boolean getGender() {
		BusUserExt ext = getUserExt();
		if (ext != null) {
			return ext.getGender();
		} else {
			return null;
		}
	}

	public Date getBirthday() {
		BusUserExt ext = getUserExt();
		if (ext != null) {
			return ext.getBirthday();
		} else {
			return null;
		}
	}

	public String getIntro() {
		BusUserExt ext = getUserExt();
		if (ext != null) {
			return ext.getIntro();
		} else {
			return null;
		}
	}

	public String getComefrom() {
		BusUserExt ext = getUserExt();
		if (ext != null) {
			return ext.getComefrom();
		} else {
			return null;
		}
	}

	public String getQq() {
		BusUserExt ext = getUserExt();
		if (ext != null) {
			return ext.getQq();
		} else {
			return null;
		}
	}

	public String getMsn() {
		BusUserExt ext = getUserExt();
		if (ext != null) {
			return ext.getMsn();
		} else {
			return null;
		}
	}

	public String getPhone() {
		BusUserExt ext = getUserExt();
		if (ext != null) {
			return ext.getPhone();
		} else {
			return null;
		}
	}

	public String getMobile() {
		BusUserExt ext = getUserExt();
		if (ext != null) {
			return ext.getMobile();
		} else {
			return null;
		}
	}
	public String getUserImg() {
		BusUserExt ext = getUserExt();
		if (ext != null) {
			return ext.getUserImg();
		} else {
			return null;
		}
	}
	public String getUserSignature() {
		BusUserExt ext = getUserExt();
		if (ext != null) {
			return ext.getUserSignature();
		} else {
			return null;
		}
	}
	
	public BusUserExt getUserExt() {
		Set<BusUserExt> set = getUserExtSet();
		if (set != null && set.size() > 0) {
			return set.iterator().next();
		} else {
			return null;
		}
	}
	
	public BusUserSite getUserSite(Integer siteId) {
		Set<BusUserSite> set = getUserSites();
		return null;
	}
	
	public Set<String> getPerms(Integer siteId,Set<String>perms) {
		if(getDisabled()){
			return null;
		}
		Set<BusUserSite> userSits=getUserSites();
		if(userSits==null){
			return null;
		}
		boolean isSuper = false;
		Set<String> allPerms = new HashSet<String>();
		if (isSuper) {
			allPerms.clear();
			allPerms.add("*");
		}
		return allPerms;
	}

	/**
	 * 是否允许上传，根据每日限额
	 * 
	 * @param size
	 * @return
	 */

	/**
	 * 是否允许上传，根据文件大小
	 * 
	 * @param size
	 *            文件大小，单位KB
	 * @return
	 */

	/**
	 * 是否允许上传后缀
	 * 
	 * @param ext
	 * @return
	 */

	public void forMember(UnifiedUser u) {
		forUser(u);
		setAdmin(false);
		setRank(0);
		setViewonlyAdmin(false);
		setSelfAdmin(false);
	}

	public void forAdmin(UnifiedUser u, boolean viewonly, boolean selfAdmin,
			int rank) {
		forUser(u);
		setAdmin(true);
		setRank(rank);
		setViewonlyAdmin(viewonly);
		setSelfAdmin(selfAdmin);
	}

	public void forUser(UnifiedUser u) {
		setStatu(USER_STATU_CHECKED);
		setId(u.getId());
		setUsername(u.getUsername());
		setEmail(u.getEmail());
		setRegisterIp(u.getRegisterIp());
		setRegisterTime(u.getRegisterTime());
		setLastLoginIp(u.getLastLoginIp());
		setLastLoginTime(u.getLastLoginTime());
		setLoginCount(0);
	}
	

	public void init() {
		if (getUploadTotal() == null) {
			setUploadTotal(0L);
		}
		if (getUploadSize() == null) {
			setUploadSize(0);
		}
		if (getUploadDate() == null) {
			setUploadDate(new java.sql.Date(System.currentTimeMillis()));
		}
		if (getAdmin() == null) {
			setAdmin(false);
		}
		if (getRank() == null) {
			setRank(0);
		}
		if (getViewonlyAdmin() == null) {
			setViewonlyAdmin(false);
		}
		if (getSelfAdmin() == null) {
			setSelfAdmin(false);
		}
		if (getStatu() == null) {
			setStatu(USER_STATU_CHECKED);
		}
		getDisabled();
	}

	public static Integer[] fetchIds(Collection<BusUser> users) {
		if (users == null) {
			return null;
		}
		Integer[] ids = new Integer[users.size()];
		int i = 0;
		for (BusUser u : users) {
			ids[i++] = u.getId();
		}
		return ids;
	}

	/**
	 * 用于排列顺序。此处优先级为0，则按ID升序排。
	 */
	public Number getPriority() {
		return 0;
	}

	/**
	 * 是否是今天。根据System.currentTimeMillis() / 1000 / 60 / 60 / 24计算。
	 * 
	 * @param date
	 * @return
	 */
	public static boolean isToday(Date date) {
		long day = date.getTime() / 1000 / 60 / 60 / 24;
		long currentDay = System.currentTimeMillis() / 1000 / 60 / 60 / 24;
		return day==(currentDay-1);
	}
	
	
	
	public boolean getDisabled(){
		Integer statu=getStatu();
		if(statu.equals(USER_STATU_DISABLED)){
			return true;
		}else{
			return false;
		}
	}

	/* [CONSTRUCTOR MARKER BEGIN] */
	public BusUser() {
		super();
	}

	/**
	 * Constructor for primary key
	 */
	public BusUser(java.lang.Integer id) {
		super(id);
	}
	

	/**
	 * Constructor for required fields
	 */
	public BusUser(java.lang.Integer id,
			java.lang.String username, java.util.Date registerTime,
			java.lang.String registerIp, java.lang.Integer loginCount,
			java.lang.Integer rank, java.lang.Long uploadTotal,
			java.lang.Integer uploadSize, java.lang.Boolean admin,
			java.lang.Boolean viewonlyAdmin, java.lang.Boolean selfAdmin,
			java.lang.Integer statu) {

		super(id, username, registerTime, registerIp, loginCount, rank,
				uploadTotal, uploadSize, admin, viewonlyAdmin, selfAdmin,
				statu);
	}

	/* [CONSTRUCTOR MARKER END] */

}